package games.gong.durid.mapper;

import games.gong.durid.entity.RolePermission;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RolePermissionMapper {
    
    @Insert("INSERT INTO role_permission(role_id, permission_id, create_time) " +
            "VALUES(#{roleId}, #{permissionId}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(RolePermission rolePermission);

    @Delete("DELETE FROM role_permission WHERE id = #{id}")
    int deleteById(Long id);
    
    @Delete("DELETE FROM role_permission WHERE role_id = #{roleId} AND permission_id = #{permissionId}")
    int deleteByRoleIdAndPermissionId(@Param("roleId") Long roleId, @Param("permissionId") Long permissionId);

    @Select("SELECT * FROM role_permission WHERE id = #{id}")
    @Results({
        @Result(property = "roleId", column = "role_id"),
        @Result(property = "permissionId", column = "permission_id"),
        @Result(property = "createTime", column = "create_time")
    })
    RolePermission selectById(Long id);
    
    @Select("SELECT * FROM role_permission WHERE role_id = #{roleId}")
    @Results({
        @Result(property = "roleId", column = "role_id"),
        @Result(property = "permissionId", column = "permission_id"),
        @Result(property = "createTime", column = "create_time")
    })
    List<RolePermission> selectByRoleId(Long roleId);
    
    @Select("SELECT * FROM role_permission WHERE permission_id = #{permissionId}")
    @Results({
        @Result(property = "roleId", column = "role_id"),
        @Result(property = "permissionId", column = "permission_id"),
        @Result(property = "createTime", column = "create_time")
    })
    List<RolePermission> selectByPermissionId(Long permissionId);
}